package LeetCode.month02.day54;

import java.util.*;

public class Test2 {

}

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] str = sc.nextLine().split(" ");
        int n = str.length;
        long[] strengths = new long[n];
        for (int i = 0; i < n; i++) {
            strengths[i] = Long.parseLong(str[i]); // 使用 long 接收大整数
        }

        List<Integer> athletes = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            athletes.add(i); // 初始运动员编号
        }

        Deque<List<Integer>> duelResult = new LinkedList<>();
        duel(athletes, strengths, duelResult);

        // 模拟比赛直到只剩一个冠军
        while (duelResult.peekFirst().size() != 1) {
            duel(duelResult.peekFirst(), strengths, duelResult);
        }

        // 将 deque 转为 list 以支持按索引访问
        List<List<Integer>> rounds = new ArrayList<>(duelResult);

        // 冠军
        int first = rounds.get(0).get(0);
        // 亚军
        int second = rounds.get(1).get(0);

        // 季军：从倒数第二轮的输家中找出最强者
        List<Integer> thirdCandidates = rounds.get(3);
        thirdCandidates.sort((a, b) ->
                strengths[a] == strengths[b] ? Integer.compare(a, b) : Long.compare(strengths[b], strengths[a])
        );
        int third = thirdCandidates.get(0);

        System.out.println(first + " " + second + " " + third);
    }

    // 模拟一轮比赛
    static void duel(List<Integer> athletes, long[] strengths, Deque<List<Integer>> duelResult) {
        List<Integer> winner = new ArrayList<>();
        List<Integer> loser = new ArrayList<>();
        int n = athletes.size();
        for (int i = 1; i < n; i += 2) {
            if (strengths[athletes.get(i)] > strengths[athletes.get(i - 1)]) {
                winner.add(athletes.get(i));
                loser.add(athletes.get(i - 1));
            } else {
                winner.add(athletes.get(i - 1));
                loser.add(athletes.get(i));
            }
        }
        // 轮空的选手自动晋级
        if (n % 2 == 1) {
            winner.add(athletes.get(n - 1));
        }
        // 将当前轮输赢队列加入双端队列头部
        duelResult.addFirst(loser);
        duelResult.addFirst(winner);
        // 只保留最近四轮记录：当前赢家、当前输家、上一轮赢家、上一轮输家
        while (duelResult.size() > 4) {
            duelResult.removeLast();
        }
    }
}
